<template>
	<!-- 首页灵活用工详情页组件 -->
	<view class="main1">
		<view class="renzheng">
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="change"></u-tabs>
			<view v-if="current==0">
				<view class="titles">
					<text class="title">请输入个人信息</text>
					<text class="subTitle">仅用于身份核验</text>
				</view>
				<u-form label-position="top" label-width="150" label-align="left" :model="form" ref="uForm">
					<u-form-item label="姓名">
						<u-input :disabled="isGrrz" v-model="form.certName" placeholder="请输入姓名" />
					</u-form-item>
					<u-form-item label="身份证号">
						<u-input :disabled="isGrrz"  v-model="form.certNo" placeholder="请输入身份证" />
					</u-form-item>
				</u-form>
			</view>
			<view v-if="current==1">
				<view class="titles">
					<text class="title">请输入企业营业执照</text>
					<text class="subTitle">仅用于身份核验</text>
				</view>
				<u-form label-position="top" label-width="150" label-align="left" :model="form" ref="uForm">
					<u-form-item label="企业名称">
						<u-input :disabled="isQyrz"  v-model="form.name" placeholder="请输入企业名称" />
					</u-form-item>
					<u-form-item label="证件号码">
						<u-input :disabled="isQyrz" v-model="form.cardNo" placeholder="请输入营业执照号码" />
					</u-form-item>
				</u-form>
			</view>
		</view>
		<view class="footer">
			<view class="f">
				<u-button v-if="current==0&&isGrrz==false" shape="circle" type="primary" @click="submit">实名认证</u-button>
				<u-button v-if="current==1&&isQyrz==false" shape="circle" type="primary" @click="submit1">企业认证</u-button>
			</view>
		</view>
		<view class="renzheng">
			<view class="titles">
				<text class="subTitle">1、信息安全保护中，认证信息将用于创建信息等功能，与账号唯一绑定，未经您允许不对外提供</text>
				<text class="subTitle">2、每个账号只能进行一次企业认证，认证成功不支持修改</text>
			</view>
		</view>
		<u-select v-model="show" @confirm="confirm" :list="listSelect"></u-select>
	</view>
</template>

<script>
	import api from '../../../../api/routes.js'
	export default {


		data() {
			return {
				isGrrz:false,
				isQyrz:false,
				form:{
					"certName": "",
					"certNo": "",
					cardNo:'',
					name:'',
				},
				list: [{
					name: '个人认证'
				}, {
					name: '企业认证'
				}],
				current: 0
			};
		},
		async onLoad(options) {
			this.getGrrz('个人')
			console.log(options.val,'类型个人or企业');
			if(options.val=='企业'){
				this.current=1;
			}else{
				this.current=0;
			}
		},

		methods: {
			async getGrrz(val){
			wx.showLoading()
			const res = await this.$myRequest({
				url: api.getAuthentication,
				method: "GET",
			});
			if (res.data.code == 0) {
				console.log(res.data.member,'获取认证')
				let  certNo = res.data.member.certNo
				let  certName = res.data.member.certName
				if(val=='个人'){
					if(!certName||!certNo){
						wx.showToast({
							title: '你还没有进行个人认证',
							icon: 'none'
						})
						this.isGrrz = false
					}else{
						this.isGrrz = true
						this.form.certName = res.data.member.certName
						this.form.certNo = res.data.member.certNo
					}
				}else{
					if(res.data.company==null){
						wx.showToast({
							title: '你还没有进行企业认证',
							icon: 'none'
						})
						this.isQyrz = false	
					}else{
						this.isQyrz = true	
						this.form.cardNo = res.data.company.cardNo
						this.form.name = res.data.company.name
					}
				}
			
				setTimeout(() => {
					wx.hideLoading()
				}, 2000)
			} else {
				wx.showToast({
					title: '认证失败',
					icon: 'none'
				})
			}	
			},
			async submit1(){
				if(!this.form.name){
					wx.showToast({
						title:'请填写企业名称',
						icon:'none'
					})
					return false
				}
				if(!this.form.cardNo){
					wx.showToast({
						title:'请填写营业执照证件号',
						icon:'none'
					})
					return false
				}
				wx.showLoading()
				const res = await this.$myRequest({
					url: api.companyCert,
					method: "POST",
					data:{
						"name": this.form.name,
						"cardNo": this.form.cardNo,
						"certType":"零工市场"
					}
				});
				if (res.data.code == 0) {	
					wx.showToast({
						title: '认证成功',
						icon: 'none'
					})
				
					setTimeout(() => {
						this.getGrrz('企业')
						wx.hideLoading()
						
					}, 1000)
					//跳转
					
					uni.navigateTo({
						url: '/package/pages/searchJob/other/addInfoDetil1',
					});
					
				} else {
					wx.showToast({
						title: '认证失败',
						icon: 'none'
					})
				}
			},
			async submit(){
				if(!this.form.certName){
					wx.showToast({
						title:'请填写姓名',
						icon:'none'
					})
					return false
				}
				if(!this.form.certNo){
					wx.showToast({
						title:'请填身份证件号',
						icon:'none'
					})
					return false
				}
				wx.showLoading()
				const res = await this.$myRequest({
					url: api.memberCert,
					method: "POST",
					data:{
						"certName": this.form.certName,
						"certNo": this.form.certNo,
					}
				});
				console.log(res,'2')
				if (res.data.code == 0) {	
					wx.showToast({
						title: '认证成功',
						icon: 'none'
					})
					
					setTimeout(() => {
						this.getGrrz('个人')
						wx.hideLoading()
					}, 2000)
				} else {
					wx.showToast({
						title: '认证失败',
						icon: 'none'
					})
				}
			},
			chooseAddress() {
				var _this = this;
				wx.chooseLocation({
					success: function(res) {
						var name = res.name
						var address = res.address
						var latitude = res.latitude
						var longitude = res.longitude
					}
				})
			},
			confirm(val) {
				console.log(val)
			},
			showSelect(val) {
				console.log(val)
				if (val == 1) {
					this.listSelect = this.list1
				} else if (val == 3) {
					this.listSelect = this.list3
				} else if (val == 4) {
					this.listSelect = this.list4
				} else if (val == 5) {
					this.listSelect = this.list5
				}
				this.show = true
			},
			change(index) {
				if(index==1&&this.isGrrz==false){
					wx.showToast({
						title: '认证企业之前必须先进行个人认证',
						icon: 'none'
					})
					return false
				}else{
					this.getGrrz('企业')
				}
				this.current = index;
			},
			goGuzhu() {
				uni.navigateTo({
					url: '/package/pages/searchJob/other/woshiguzhu',
				});
			},
			goLinggong() {
				uni.navigateTo({
					url: '/package/pages/searchJob/other/woshilinggong',
				});
			},
			lgRegister() {
				console.log("林工登记！！！");
				uni.navigateTo({
					url: '/package/pages/components/lingGonRegister',
				});
			}
		},
	};
</script>

<!-- <style>
/* #ifndef H5 */
page {
  height: 100%;
  background-color: #f2f2f2;
}

/* #endif */
</style> -->

<style lang="scss" scoped>
	.main1 {
		background: white;

		.renzheng {
			width: 94%;
			margin-left: 3%;
			background: white;

			.titles {
				display: flex;
				flex-direction: column;
				margin-top: 50rpx;

				.title {
					font-family: Arial, Helvetica, sans-serif;
					font-size: 28rpx;
				}

				.subTitle {
					color: #909399;
					font-size: 24rpx;
					;
				}
			}
		}

		.footer {
			display: flex;
			align-items: center;

			.f {
				width: 90%;
				margin-left: 5%;
			}

			height: 120rpx;
			background: white;
			width: 100%;
		}

		.geren {
			.qzxx {
				box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
				width: 90%;
				margin-left: 5%;
				background: white;
				border-radius: 6px;
				margin-top: 20rpx;
				padding-top: 20rpx;
				padding-bottom: 20rpx;

				.form {
					width: 90%;
					margin-left: 20rpx;
				}

				.title {
					margin-left: 20rpx;
				}
			}

			.info {
				box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
				width: 90%;
				margin-left: 5%;
				background: white;
				border-radius: 6px;
				min-height: 280rpx;
				margin-top: 30rpx;
				padding-top: 20rpx;

				.tel {
					width: 94%;
					margin-left: 3%;
					display: flex;
					align-items: center;
					border-radius: 4px;
					height: 70rpx;
					margin-top: 45rpx;
					background: rgb(224, 230, 241);

					.icon {
						margin-left: 20rpx;
					}
				}

				.userInfo {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.left {
						display: flex;
						flex-direction: column;
						margin-left: 20rpx;

						.name {
							font-size: 32rpx;
							font-family: Arial, Helvetica, sans-serif;
							font-weight: bold;
						}
					}

					.right {
						image {
							margin-right: 20rpx;
							height: 100rpx;
							width: 100rpx;
						}
					}
				}
			}
		}
	}
</style>
